package com.staginfo.segs.ui.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.staginfo.segs.R;
import com.staginfo.segs.common.Constants;
import com.staginfo.segs.ui.activity.DevDetailsActivity;
import com.staginfo.segs.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

import static android.view.View.inflate;


/**
 * Created by robert.luozizhao on 2017/9/4 0004.
 */

public class HomeFragment extends Fragment {

    @Bind(R.id.lv_warning)
    ListView mLvWarning;
    List<String> mList = new ArrayList<>();  // 直接从平台获取全部警告
    List<String> mCurrentShowList = new ArrayList<>();// 当前listview显示警告

    public BaseAdapter mBaseAdapter = new MyBaseAdapter();


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_warning, null);
        ButterKnife.bind(this, rootView);
        return rootView;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
/*        mWarningPagerAdapter = new WarningPagerAdapter(getChildFragmentManager(),getActivity(),this);
        mWarningPagerAdapter.setFragmentList(mFragmentList);
        mViewPager.setAdapter(mWarningPagerAdapter);
        mTabLayout.setupWithViewPager(mViewPager);
        mViewPager.addOnPageChangeListener(mOnPageChangerListener);*/
        initView();
        initData();
        initEvent();
    }



    private void initView() {
        mLvWarning.setAdapter(mBaseAdapter);
    }

    private void initData() {
        if (mList.size()==0) {
            for (int i = 0; i < 30; i++) {
                mList.add("item --> " + i);
            }
        }
        // 默认个人告警
        setWarningType(Constants.WARNING_TYPE_PER);
    }

    private void initEvent() {
        mLvWarning.setOnItemClickListener(mOnItemClickListener);
    }


    private AdapterView.OnItemClickListener mOnItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            ToastUtil.getInstance(getActivity()).shortToast("点击了第 " + position + " Item");
            // 跳转 设备详情页面
            Intent intent = new Intent(getActivity(), DevDetailsActivity.class);
            startActivity(intent);
        }
    };



    public class MyBaseAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            if (mCurrentShowList.size() > 0) {
                return mCurrentShowList.size();
            }
            return 0;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = inflate(getActivity(), R.layout.tv_test, null);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.tv.setText(mCurrentShowList.get(position));
            return convertView;
        }
    }


    public void setWarningType(int warningType) {
        //listview的数据变化
        refreshData(warningType);
        mBaseAdapter.notifyDataSetChanged();
    }



    private void refreshData(int warningType) {

        mCurrentShowList.clear();

        switch (warningType){
            case Constants.WARNING_TYPE_PER:
                for (int i = 0; i < mList.size(); i++) {
                    if (i%2==0) {
                        mCurrentShowList.add(mList.get(i));
                    }
                }
                break;
            case Constants.WARNING_TYPE_ALL:
                for (int i = 0; i < mList.size(); i++) {
                    if (i%3==0) {
                        mCurrentShowList.add(mList.get(i));
                    }
                }
                break;
        }
    }


    public class ViewHolder {
        TextView tv;
        ViewHolder(View v) {
            tv = (TextView) v.findViewById(R.id.tv_test01);
        }
    }



    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
